<?php
/**
 * Created by PhpStorm.
 * User: meiyun1
 * Date: 2017/2/23
 * Time: 下午2:03
 */

namespace App\Models\ActivityModel;

use App\Contracts\Repositories\ActivityModelRepository as ActivityModelContractsRepository;

class ActivityModelRepository implements ActivityModelContractsRepository
{
    public function create(ActivityModel $activityModel)
    {
        // TODO: Implement create() method.
        if (!is_null($activityModel->id)) {
            throw new \Exception('duplicate id');
        }
        if ($activityModel->save()) {
            return $activityModel;
        }
        return false;
    }

    public function findAll()
    {
        // TODO: Implement findAll() method.

        return ActivityModel::query()->get();
    }

    public function findOne($id)
    {
        // TODO: Implement findOne() method.

        return ActivityModel::query()->find($id);
    }

    /**
     * {@inheritdoc}
     * @see \App\Contracts\Repositories\ActivityModelRepository::findByRule()
     */
    public function findByRule($rule)
    {
        return ActivityModel::query()->where('rule', $rule)->first();
    }

    public function findByConditions($condition)
    {
        $qb = ActivityModel::where($condition)->with('Activity');

        //        return $qb->get();
        return $qb->get();
    }
}
